<?php
include_once "mdbtable.php";

class MKurs extends MDbTable {

	public $table = "kurs";

	public function getList($filters = array())
	{
		if(!isset($filters['sort']))
		{
			$filters['sort'] = 'date asc';
		}
		$rows = parent::getList($filters);
		return $rows;
	}

	protected function _modifyRow(&$row)
	{
		list($year, $month, $day) = explode("-", $row['date']);
		$row['day'] = intval($day);
		$row['month'] = intval($month);
		$row['year'] = intval($year);
		parent::_modifyRow($row);
	}

	public function getKurs($date, $percent, $type)
	{
		$date = dateToDb($date);
		$filters = array('date<=' => $date, 'count' => 1, 'percent' => $percent, 'sort' => 'date desc');
		$rows = $this->getList($filters);
		$row = reset($rows);
		$out = $row[$type];
		if($type == 'sell' && !$out)
		{
			$filters2 = array('date<=' => $date, 'count' => 2, 'percent' => $percent, 'sort' => 'date desc');
			$rows2 = $this->getList($filters2);
			$row2 = end($rows2);
			$out = $row2['buy'];
		}
		return $out;
	}

	public function updateKurs($arr)
	{
		$query = $this->db->insert_string($this->table, $arr);
		if($arr['date'] > date('Y-m-d'))
		{
			$query .= ' ON DUPLICATE KEY UPDATE `buy` = values(`buy`), `sell` = values(`sell`)';
		}
		else
		{
			$query .= ' ON DUPLICATE KEY UPDATE id = id';
		}
		$this->db->query($query);
	}

	public function addExchange($valuta, $date, $kurs, $nominal)
	{
		$date = dateToDb($date);
		$arr = array(
			'valuta' => $valuta,
			'date' => $date,
			'kurs' => $kurs,
			'nominal' => $nominal,
		);
		$query = $this->db->insert_string("exchange", $arr);
		$query .= ' ON DUPLICATE KEY UPDATE kurs = values(kurs)';
		$this->db->query($query);
	}

	public function getAllExchange()
	{
		$this->db->order_by('date', 'desc');
		return $this->db->get('exchange')->result_array();
	}

	public function getExchange($date, $v1, $v2)
	{
		$out = 1;
		if($v1 == $v2) return $out;
		$date = dateToDb($date);
		if($v1 == 'RUB' && $v2 != 'RUB')
		{
			$this->db->order_by('date', 'desc');
			$row = $this->db->get_where('exchange', array('date<=' => $date, 'valuta' => $v2))->first_row('array');
			if(count($row) && @$row['kurs'])
			{
				$out = 1 / ($row['kurs'] / $row['nominal']);
			}
		}
		if($v1 != 'RUB' && $v2 == 'RUB')
		{
			$this->db->order_by('date', 'desc');
			$row = $this->db->get_where('exchange', array('date<=' => $date, 'valuta' => $v1))->first_row('array');
			if(count($row) && @$row['kurs'])
			{
				$out = $row['kurs'] / $row['nominal'];
			}
		}
		if($v1 != 'RUB' && $v2 != 'RUB')
		{
			$this->db->order_by('date', 'desc');
			$row1 = $this->db->get_where('exchange', array('date<=' => $date, 'valuta' => $v1))->first_row('array');
			$this->db->order_by('date', 'desc');
			$row2 = $this->db->get_where('exchange', array('date<=' => $date, 'valuta' => $v2))->first_row('array');
			if(count($row1) && @$row1['kurs'] && count($row2) && @$row2['kurs'])
			{
				$out = ($row1['kurs'] / $row1['nominal']) / ($row2['kurs'] / $row2['nominal']);
			}
		}
		$out = round($out, 10);
		return $out;
	}



}

